package com.zt.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zt.reggie.common.BaseContext;
import com.zt.reggie.common.R;
import com.zt.reggie.entity.Dish;
import com.zt.reggie.entity.ShoppingCart;
import com.zt.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec com.zt.reggie.controller
 * @since 2023 - 02 - 28 - 15:48
 */
@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加菜品/套餐
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("shoppingCart:{}", shoppingCart.toString());

        //设置用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        //判断添加的是套餐还是菜品
        if(shoppingCart.getDishId() != null){
            //添加的是菜品
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }else{
            //添加的是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //判断该用户是否点过该菜品/套餐
        ShoppingCart cart = shoppingCartService.getOne(wrapper);
        if(cart != null){
            // number + 1
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartService.updateById(cart);
        }else{
            // 添加
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cart = shoppingCart;
        }
        return R.success(cart);
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车");
        return R.success(shoppingCartService.list(BaseContext.getCurrentId()));
    }

    /**
     * 删除菜品/套餐
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> update(@RequestBody ShoppingCart shoppingCart){
        log.info("ShoppingCart-sub:{}", shoppingCart.toString());

        //设置用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        //判断添加的是套餐还是菜品
        if(shoppingCart.getDishId() != null){
            //添加的是菜品
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }else{
            //添加的是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //判断该用户购物车中是否只剩一件
        ShoppingCart cart = shoppingCartService.getOne(wrapper);
        // number - 1
        cart.setNumber(cart.getNumber() - 1);
        if(cart.getNumber() > 0){
            shoppingCartService.updateById(cart);
        }else{
            // 移除
            shoppingCartService.remove(wrapper);
        }
        return R.success(cart);
    }
    @DeleteMapping("/clean")
    public R<String> clean(){
        shoppingCartService.clean();
        return R.success("清空购物车成功!");
    }
}
